package com.hellogood.yaohuo.client.basic;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.yaohuo.shared.entity.Ziyou;
import com.hellogood.yaohuo.shared.services.ZiyouService;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;

public class ZiyouController {

	private TreeNode[] treeNodes = null;

	private HLayout layout = null;
	private TreeGrid treeGrid = null;
	private Button addBtn = null;
	private Button modifyBtn = null;

	public ZiyouController() {
		initData();
		initView();
	}

	public void initData() {
		ZiyouService.Util.getInstance().getAllZiyou(new GetAllAsyncCallback());
	}

	private void initView() {
		TreeGridField codeField = new TreeGridField("code", "编码");
		codeField.setHidden(true);
		TreeGridField nameField = new TreeGridField("name", "名称");
		TreeGridField superCodeField = new TreeGridField("superCode");
		superCodeField.setHidden(true);
		treeGrid = new TreeGrid();
		treeGrid.setFields(codeField, nameField, superCodeField);
		treeGrid.setWidth("70%");
		treeGrid.addSelectionChangedHandler(new TreeSelectionHandler());

		addBtn = new Button("增加项目");
		addBtn.setSize("100", "30");
		addBtn.addClickHandler(new AddBtnClickHandler());

		modifyBtn = new Button("修改项目");
		modifyBtn.setDisabled(true);
		modifyBtn.setSize("100", "30");
		//
		modifyBtn.addClickHandler(new ModifyBtnClickHandler());

		VLayout btnLayout = new VLayout();
		btnLayout.setMembers(addBtn, modifyBtn);
		btnLayout.setMembersMargin(10);
		btnLayout.setHeight(500);
		btnLayout.setWidth("30%");

		layout = new HLayout();
		layout.addMember(treeGrid);
		layout.addMember(btnLayout);
		layout.setWidth100();
		layout.setHeight100();
	}

	public Canvas getCanvas() {
		return layout;
	}

	private class GetAllAsyncCallback implements AsyncCallback<List<Ziyou>> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(List<Ziyou> result) {
			if (result != null) {
				treeNodes = new TreeNode[result.size()];
				for (int i = 0; i < result.size(); i++) {
					Ziyou c = result.get(i);
					TreeNode r = new TreeNode();
					r.setAttribute("nodeId", c.getZiyouCode());
					r.setAttribute("code", c.getZiyouCode());
					r.setAttribute("name", c.getZiyouName());
					r.setAttribute("isOpen", true);
					treeNodes[i] = r;
				}

				Tree tree = new Tree();
				tree.setModelType(TreeModelType.PARENT);
				tree.setNameProperty("name");
				tree.setRootValue("0");
				tree.setIdField("nodeId");
				tree.setParentIdField("parentId");
				tree.setOpenProperty("isOpen");
				tree.setData(treeNodes);

				treeGrid.setData(tree);
			}
		}
	}

	private class TreeSelectionHandler implements SelectionChangedHandler {

		@Override
		public void onSelectionChanged(SelectionEvent event) {
			ListGridRecord r = treeGrid.getSelectedRecord();
			if (r != null) {
				modifyBtn.setDisabled(false);

			} else {
				modifyBtn.setDisabled(true);
			}
		}
	}

	private class AddBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			ZiyouModifyWindow window = new ZiyouModifyWindow(ZiyouController.this, null, false);
			window.setSize("300", "200");
			window.centerInPage();
			window.show();
			window.getTextItem().focusInItem();
		}
	}

	private class ModifyBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			Record r = treeGrid.getSelectedRecord();
			if (r != null) {
				Ziyou ziyou = new Ziyou();
				ziyou.setZiyouCode(r.getAttribute("code"));
				ziyou.setZiyouName(r.getAttribute("name"));
				ZiyouModifyWindow window = new ZiyouModifyWindow(ZiyouController.this, ziyou, true);
				window.setSize("300", "200");
				window.centerInPage();
				window.show();
				window.getTextItem().focusInItem();
			}
		}
	}
}
